package base.http.utils

import io.reactivex.subscribers.DisposableSubscriber

class SubscriberUtils {

    companion object {
        val instance = SubscriberUtils()
        val subscribers = mutableSetOf<DisposableSubscriber<*>>()
    }

    fun addSubscriber(disposable: DisposableSubscriber<*>) {
        subscribers.add(disposable)
    }

    fun cancelAll() {
        val it = subscribers.iterator()
        while (it.hasNext()) {
            var item: DisposableSubscriber<*>? = it.next()
            item!!.dispose()
            it.remove()
            item = null
        }
    }

    fun cancel(clazz: String){
        val it = subscribers.iterator()
        while (it.hasNext()) {
            var item: DisposableSubscriber<*>? = it.next()
            if(item?.javaClass?.name!!.contains(clazz)) {
                item!!.dispose()
                it.remove()
                item = null
            }
        }
    }
}